package com.shop.service.impl;

import com.shop.mapper.AuctionCustomerMapper;
import com.shop.mapper.AuctionMapper;
import com.shop.mapper.AuctionrecordMapper;
import com.shop.pojo.Auction;
import com.shop.pojo.AuctionCustomer;
import com.shop.pojo.AuctionExample;
import com.shop.pojo.Auctionrecord;
import com.shop.service.AuctionService;
import com.shop.tools.AuctionCustomerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
@Service
public class AuctionServiceImpl implements AuctionService {
    @Autowired
    private AuctionMapper auctionMapper;
    //自定义mapper
    @Autowired
    private AuctionCustomerMapper auctionCustomerMapper;
    @Autowired
    private AuctionrecordMapper auctionrecordMapper;

//    @Override
//    public List<Auction> findAuctionList() {
//        return this.auctionMapper.selectByExample(null);
//    }

    @Override
    public List<Auction> findAuctionList(Auction auction) {
        AuctionExample example = new AuctionExample();
        AuctionExample.Criteria criteria = example.createCriteria();
        if (auction!=null){
            //判断拍卖查询是否输入了名称
            if (auction.getAuctionname()!=null&&!"".equals(auction.getAuctionname())){
                criteria.andAuctionnameLike("%"+auction.getAuctionname()+"%");
            }
            //判断查询是否输入描述
            if (auction.getAuctiondesc()!=null&&!"".equals(auction.getAuctiondesc())){
                criteria.andAuctiondescEqualTo(auction.getAuctiondesc());
            }
            //判断查询是否输入拍卖的开始时间
            if (auction.getAuctionstarttime()!=null){
                //大于开始时间判断
                criteria.andAuctionstarttimeGreaterThan(auction.getAuctionstarttime());
            }
            //查询结束时间
            if (auction.getAuctionendtime()!=null){
                //小于结束时间判断
                criteria.andAuctionendtimeLessThan(auction.getAuctionendtime());
            }
            //查询起拍价
            if (auction.getAuctionstartprice()!=null){
                criteria.andAuctionstartpriceGreaterThan(auction.getAuctionstartprice());
            }
        }
        return this.auctionMapper.selectByExample(example);
    }

    @Override
    public Auction selectAuctionAndRecorList(String auctionId) {
        return this.auctionCustomerMapper.selectAuctionAndRecorList(auctionId);
    }
/*
* 时间不能晚于结束时间
* 竞拍价格要高于竞拍记录价格的最高价
* 竞拍价格要高于起拍价
* */
    @Override
    public void saveAuctionRecord(Auctionrecord auctionrecord) throws Exception {
       /*
       时间不能晚于结束时间
       */
        String s = String.valueOf(auctionrecord.getAuctionid());
        Auction auction = this.auctionCustomerMapper.selectAuctionAndRecorList(s);
        //时间比较 结束时间和我的当前访问时间
        if (new Date().after(auction.getAuctionendtime())){
            throw new AuctionCustomerException("该竞拍已结束");
        }
        if(auction.getAuctionrecodList().size()>0&&auction.getAuctionrecodList()!=null){
            //存在竞拍记录
            //获取所有竞拍的最高价格记录
            Auctionrecord maxAuctionrecord = auction.getAuctionrecodList().get(0);
            //获取最高价格
            if (auctionrecord.getAuctionprice().compareTo(maxAuctionrecord.getAuctionprice())<1)
            {
                throw new AuctionCustomerException("你的竞拍价格不得低于最高价");
            }
        }
        else {
            //不存在竞拍记录
            //你的竞拍价格要高于起拍价
            if (auctionrecord.getAuctionprice().compareTo(auction.getAuctionstartprice())<1){
                throw new AuctionCustomerException("你的竞拍价格不得低于起拍价");
            }
        }

        this.auctionrecordMapper.insert(auctionrecord);
    }

    @Override
    public List<AuctionCustomer> selectAuctionEndTime() {
        return this.auctionCustomerMapper.selectAuctionEndTime();
    }

    @Override
    public List<Auction> selectAuctionNoEndTime() {
        return this.auctionCustomerMapper.selectAuctionNoEndTime();
    }

    @Override
    public void publishAuctions(Auction auction) {
        this.auctionMapper.insert(auction);
    }
}
